package com.nowadays.mobile.ui;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.nowadays.mobile.R;

import net.xpece.android.support.preference.SeekBarDialogPreference;

public class SettingActivity extends AppCompatActivity implements PreferenceFragmentCompat.OnPreferenceDisplayDialogCallback {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_setting);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null) {
      toolbar.setTitle(R.string.title_activity_setting);
      toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.primary));
      setSupportActionBar(toolbar);

      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    if (savedInstanceState == null) {
      SettingFragment fragment = new SettingFragment();
      getSupportFragmentManager().beginTransaction().add(R.id.container, fragment, "Settings").commit();
    }
  }

  @Override
  public boolean onOptionsItemSelected(final MenuItem item) {
    int id = item.getItemId();
    switch (id) {
      case android.R.id.home: {
        super.onBackPressed();
        return true;
      }
    }
    return super.onOptionsItemSelected(item);
  }

  @Override
  public boolean onPreferenceDisplayDialog(PreferenceFragmentCompat preferenceFragmentCompat, Preference preference) {
    if (preference instanceof SeekBarDialogPreference) {
      final String key = preference.getKey();
      DialogFragment f = XpNumberSeekBarPreferenceDialogFragment.newInstance(key);
      f.setTargetFragment(preferenceFragmentCompat, 0);
      f.show(this.getSupportFragmentManager(), key);
      return true;
    }
    return false;
  }
}
